package com.partner.frame;

import java.text.*;
import java.util.*;

import javax.servlet.http.*;

import org.springframework.web.bind.*;
import org.springframework.web.servlet.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.*;
import org.springframework.beans.propertyeditors.*;

import com.partner.frame.common.ConstantsMark;
import com.partner.frame.support.util.*;

public class BaseAction
{
   public final static String ACTION_RESULT = "resultStatus";
   public final static String ACTION_MESSAGE = "resultMessage";

   @InitBinder
   protected void initBinder(WebDataBinder binder)
   {
      binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
   }
   @ExceptionHandler
   public ModelAndView exp(HttpServletRequest request, Exception ex) { 
	   ModelAndView  mv = new ModelAndView("commons/exception") ;
	   ex.printStackTrace();
       mv.addObject("ex", ex);
       return mv; //返回Exception.jsp页面
   }
   //获取IP地址
   public String getIpAddr(HttpServletRequest request)
   {
      String strHostIP = request.getHeader("x-forwarded-for");
      if(strHostIP == null || strHostIP.length() == 0 || "unknown".equalsIgnoreCase(strHostIP))
      {
         strHostIP = request.getHeader("Proxy-Client-IP");
      }
      if(strHostIP == null || strHostIP.length() == 0 || "unknown".equalsIgnoreCase(strHostIP))
      {
         strHostIP = request.getHeader("WL-Proxy-Client-IP");
      }
      if(strHostIP == null || strHostIP.length() == 0 || "unknown".equalsIgnoreCase(strHostIP))
      {
         strHostIP = request.getRemoteAddr();
      }
      return strHostIP;
   }

   //所有ActionMap 统一从这里获取
   public Map<String, Object> getRootMap()
   {
      String ctxPath = "", basePath = "", webUrl = "";
      Map<String, Object> rootMap = null;
      
      rootMap = new HashMap<String, Object>();
      ctxPath = getRequest().getContextPath();
      
      basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + ctxPath + "/";
      webUrl = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + "/web";
      String easyuiUrl = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + ctxPath + "/resources/js/jquery-easyui-1.3.2";
      String imgUrl = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + "/";
      String uploadUrl = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + ctxPath + "/";
      /** 应用部署绝对路径 */
      String systemPath = getRequest().getSession().getServletContext().getRealPath("");

      rootMap.put("msUrl", basePath);
      rootMap.put("webUrl", webUrl);
      rootMap.put("easyuiUrl", easyuiUrl);
      rootMap.put("imgUrl", imgUrl);
      rootMap.put("uploadUrl", uploadUrl);
      rootMap.put("systemPath", systemPath);

      // sysWebPath=D:/apache-tomcat/apache-tomcat-7.0.29/webapps/
      // rootMap.putAll(URLUtils.getUrlMap());
      return rootMap;
   }

   public ModelAndView forword(String viewName, Map<String, ?> context)
   {
      return new ModelAndView(viewName, context);
   }

   public ModelAndView error(String errorMessage)
   {
      return new ModelAndView("error");
   }

   /**
    * 提示成功信息
    */
   public void sendSuccessMessage(HttpServletResponse response, String message)
   {
      Map<String, Object> mapResult = new HashMap<String, Object>();
      
      mapResult.put(ACTION_RESULT, true);
      mapResult.put(ACTION_MESSAGE, message);
      JsonUtil.outJson(mapResult, response);
   }

   //提示失败信息
   public void sendFailureMessage(HttpServletResponse response, String message)
   {
      Map<String, Object> mapResult = new HashMap<String, Object>();
      
      mapResult.put(ACTION_RESULT, false);
      mapResult.put(ACTION_MESSAGE, message);
      JsonUtil.outJson(mapResult, response);
   }

   // @Auth(verifyLogin=false,verifyURL=false)
/*   @RequestMapping("/pageUrl")
   public ModelAndView pageUrl(HttpServletRequest request, HttpServletResponse response)
   {
      Map<String, Object> context = getRootMap();
      String pageUrl = request.getParameter("pageUrl");
      
      return forword(pageUrl, context);
   }*/

   public HttpServletRequest getRequest()
   {
      HttpSession userSession = null;
      Map<String, Object> mapParam = null;
      HttpServletRequest httpRequest = null;
      String paramName = "", paramValue = "";
      Enumeration<String> enumParam = null, enumHeader = null;

      // 处理所有参数
      System.out.println("------------------------Parameter------------------------");
      //httpRequest = ServletActionContext.getRequest();
      httpRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
      userSession = httpRequest.getSession(false);
      if(userSession != null)
         System.out.println("userSession = " + userSession.toString());
      else
         System.out.println("userSession = null");
      
      enumParam = httpRequest.getParameterNames();
      mapParam = new HashMap<String, Object>();
      while(enumParam.hasMoreElements())
      {
         paramName = enumParam.nextElement();
         paramValue = httpRequest.getParameter(paramName);
         mapParam.put(paramName, paramValue);
         
         if(paramValue != null && paramValue.length() > 512)
            System.out.println(paramName + " = " + paramValue.substring(0, 64) + " Length[" + paramValue.length() + "]");
         else
            System.out.println(paramName + " = " + paramValue);
      }
      System.out.println("------------------------Parameter------------------------\n");
      
      // 获取请求头信息
      System.out.println("------------------------header------------------------");
      enumHeader = httpRequest.getHeaderNames();
      while(enumHeader.hasMoreElements())
      {
         paramName = (String) enumHeader.nextElement();
         paramValue = httpRequest.getHeader(paramName);
         System.out.println(paramName + " = " + paramValue);
      }
      System.out.println("------------------------header------------------------\n");

      return httpRequest;
   }

   public Map<String, Object> getParameterMap()
   {
      HttpSession userSession = null;
      Map<String, Object> mapParam = null;
      HttpServletRequest httpRequest = null;
      String paramName = "", paramValue = "";
      Enumeration<String> enumParam = null, enumHeader = null;

      // httpRequest = ServletActionContext.getRequest();
      httpRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
      userSession = httpRequest.getSession(false);
      if(userSession != null) System.out.println("userSession = " + userSession.toString());
      else System.out.println("userSession = null");

      // 处理所有参数
      enumParam = httpRequest.getParameterNames();
      mapParam = new HashMap<String, Object>();
      System.out.println("------------------------Parameter------------------------");
      while(enumParam.hasMoreElements())
      {
         paramName = enumParam.nextElement();
         paramValue = httpRequest.getParameter(paramName);
         mapParam.put(paramName, paramValue);

         if(paramValue != null && paramValue.length() > 512) System.out.println(paramName + " = " + paramValue.substring(0, 64) + " Length[" + paramValue.length() + "]");
         else System.out.println(paramName + " = " + paramValue);
      }
      System.out.println("------------------------Parameter------------------------\n");

      // 获取请求头信息
      System.out.println("------------------------header------------------------");
      enumHeader = httpRequest.getHeaderNames();
      while(enumHeader.hasMoreElements())
      {
         paramName = (String) enumHeader.nextElement();
         paramValue = httpRequest.getHeader(paramName);
         System.out.println(paramName + " = " + paramValue);
      }
      System.out.println("------------------------header------------------------\n");

      return mapParam;
   }

   public Map<String, Object> getParameterMap(HttpServletRequest httpRequest)
   {
      HttpSession userSession = null;
      Map<String, Object> mapParam = null;
      String paramName = "", paramValue = "";
      Enumeration<String> enumParam = null, enumHeader = null;

      // httpRequest = ServletActionContext.getRequest();
      httpRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
      userSession = httpRequest.getSession(false);
      if(userSession != null) System.out.println("userSession = " + userSession.toString());
      else System.out.println("userSession = null");

      // 处理所有参数
      enumParam = httpRequest.getParameterNames();
      mapParam = new HashMap<String, Object>();
      System.out.println("------------------------Parameter------------------------");
      while(enumParam.hasMoreElements())
      {
         paramName = enumParam.nextElement();
         paramValue = httpRequest.getParameter(paramName);
         mapParam.put(paramName, paramValue);

         if(paramValue != null && paramValue.length() > 512) System.out.println(paramName + " = " + paramValue.substring(0, 64) + " Length[" + paramValue.length() + "]");
         else System.out.println(paramName + " = " + paramValue);
      }
      System.out.println("------------------------Parameter------------------------");

      // 获取请求头信息
      System.out.println("------------------------header------------------------");
      enumHeader = httpRequest.getHeaderNames();
      while(enumHeader.hasMoreElements())
      {
         paramName = (String) enumHeader.nextElement();
         paramValue = httpRequest.getHeader(paramName);
         System.out.println(paramName + " = " + paramValue);
         mapParam.put(paramName, paramValue);
      }
      System.out.println("------------------------header------------------------\n");

      return mapParam;
   }
   
}
